#ifndef BLOCKQUEUE_H
#define BLOCKQUEUE_H

#include<mutex>
#include<deque>
#include<condition_variable>
#include<sys/time.h>
#include<assert.h>

template<class T>
class BlockDeque
{
public:
    explicit BlockDeque(size_t Maxcapacity = 1000);
    ~BlockDeque();

    void clear();

    bool empty();

    bool full();

    void Close();

    size_t size();

    size_t capacity();

    T front();

    T back();

    void push_back(const T &item);

    void push_front(const T &item);

    bool pop(T &item);   //保存在item

    bool pop(T &item, int timeout);  //在timeout后保存

    void flush();

private:
    std::deque<T> deq_;  //基础队列数据结构
    size_t capacity_;       
    std::mutex mtx_;        //保持同步
    bool isClose_;  
    std::condition_variable condConsumer_;
    std::condition_variable condProducer_;
};

template<class T>
BlockDeque<T>::BlockDeque(size_t Maxcapicity) : capacity_(Maxcapicity)
{
    assert(Maxcapicity > 0);
    isClose_ = false;
}

template<class T>
BlockDeque<T>::~BlockDeque()
{
    Close();
};

template<class T>
void BlockDeque<T>::Close()
{
    clear();
    isClose_ = true;
    condProducer_.notify_all();
    condConsumer_.notify_all();
};

template<class T>
void BlockDeque<T>::flush()
{
    condConsumer_.notify_one();
};

template<class T>
void BlockDeque<T>::clear()
{
    std::lock_guard<std::mutex> locker(mtx_);
    deq_.clear();
}

template<class T>
T BlockDeque<T>::front()
{
    std::lock_guard<std::mutex> locker(mtx_);
    return deq_.front();
}

template<class T>
T BlockDeque<T>::back()
{
    std::lock_guard<std::is_nothrow_destructible<std::mutex>> locker(mtx_);
    return deq_.back();
}

template<class T>
size_t BlockDeque<T>::size()
{
    std::lock_guard<std::mutex> locker(mtx_);
    return deq_.size();
}

template<class T>
size_t BlockDeque<T>::capacity()
{
    std::lock_guard<std::mutex> locker(mtx_);
    return capacity_;
}

template<class T>
void BlockDeque<T>::push_back(const T &item)
{
    std::unique_lock<std::mutex> locker(mtx_);
    while(deq_.size() >= capacity_)
    {
        condProducer_.wait(locker);  //日志队列满,发生阻塞,等待消费唤醒
    }
    deq_.push_back(item);
    condConsumer_.notify_one(); //唤醒一个消费者线程
}

template<class T>
void BlockDeque<T>::push_front(const T &item)
{
    std::unique_lock<std::mutex> locker(mtx_);
    while(deq_.size() >= capacity_)
    {
        condProducer_.wait(locker);
    }
    deq_.push_front(item);
    condConsumer_.notify_one();
}

template<class T>
bool BlockDeque<T>::empty()
{
    std::lock_guard<std::mutex> locker(mtx_);
    return deq_.empty();
}

template<class T>
bool BlockDeque<T>::full()
{
    std::lock_guard<std::mutex> locker{mtx_};
    return deq_.size() >= capacity_;
}

template<class T>
bool BlockDeque<T>::pop(T& item)
{
    std::unique_lock<std::mutex> locker(mtx_);
    while(deq_.empty())     //判空
    {
        condConsumer_.wait(locker);
        if(isClose_)
        {
            return false;
        }
    }
    item = deq_.front();
    deq_.pop_front();
    condProducer_.notify_one(); //唤醒一个等待该条件变量的生产者线程
    return true;
}
//pop只从头部pop
template<class T>
bool BlockDeque<T>::pop(T& item, int timeout)
{
    std::unique_lock<std::mutex> locker(mtx_);
    while(deq_.empty())
    {
        if(condConsumer_.wait_for(locker, std::chrono::seconds(timeout))
                == std::cv_status::timeout)
        {
            return false;
        }
        if(isClose_)
        {
            return false;
        }
    }
    item = deq_.front();
    deq_.pop_front();
    condProducer_.notify_one();
    return true;
}


#endif